import  BaseUtils from './BaseUtils'

export default {
  mixins: { BaseUtils},
  components: {},
  props: {
    pageParams: {
      type: Object,
      default: () => {
        return {}
      },
    },
    pageHeight: Number,
    pageWidth: Number,
  },
  data() {
    return {
      helpVisible: false,
      form: null,
      defaultValues: {},
      formData: { // 前端表单数据
        id: '',
      },
      entityData: null, // 来自后台的数据对象
      loading: false,
      submitting: false,
      deleting: false,
      loadUrl: '',
      addUrl: '',
      updateUrl: '',
      deleteUrl: '',
      loadEntityData: window.$debounce(this._loadEntityData),
      message: ''
    }
  },
  computed: {
    saveButtonText() {
      return this.pageParams.id ? '确定：修改' : '确定：新增'
    },
    submitUrl() {
      if (this.pageParams.id) {
        return this.updateUrl + this.pageParams.id
      }
      return this.addUrl
    },
    formLabelCol() {
      return { span: 4 }
    },
    formWrapperCol() {
      return { span: 18 }
    }
  },
  mounted() {
    this.init()
  },
  watch: {
    pageParams() {
      this.init()
    },
    entityData(obj) {
      this.initFormData(obj)
      this._validateForm();
    },
  },
  methods: {
    // 子类可以重写该函数
    init() {
      this._initMe()
    },
    _initMe() {
      if (this.pageParams.id) {
        this.loadEntityData()
      } else {
        this.initFormData(this.pageParams)
        this._clearValidateForm();
      }
    },
    // 子类可以重写该函数
    initFormData(obj) {
      this._initFormData(obj)
    },
    _initFormData(obj) {
      if (obj) {
        for(let fld in this.formData) {
          this.$set(this.formData, fld, obj[fld] || this.defaultValues[fld])
        }
      } else {
        for(let fld in this.formData) {
          this.$set(this.formData, fld, this.defaultValues[fld])
        }
      }
    },
    _loadEntityData() {
      debugger
      this.loading = true
      this.$http.get(this.loadUrl + this.pageParams.id).then((rs) => {
        this.loading = false
        //
        const result = rs.data
        if (result.success) {
          this.entityData = result.data
        } else {
          this.entityData = null
        }
      })
    },
    _validateForm() {
      if (!this.$refs.form) {
        return true
      }
      let isValid = false
      this.$refs.form.validate(valid => {
        if (valid) {
          isValid = true
        }
      });
      return isValid
    },
    _clearValidateForm() {
      if (this.$refs.form) {
        this.$refs.form.clearValidate()
      }
    },
    // 子类可以重写该函数
    doSubmit() {
      if (this._validateForm()) {
        this._doSubmit()
      }
    },
    _doSubmit(callback) {
      this.submitting = { delay: 50 }
      this.$http.post(this.submitUrl, this.formData).then((rs) => {
        this.submitting = false
        //
        const result = rs.data
        if (result.success) {
          this.$message.success(result.message);
          this.doSuccess(result.data)
          //
          if (!this.pageParams.id) {
            this.initFormData(this.pageParams)
            this._clearValidateForm()
          }
          callback ? callback() : () => {}
        } else {
          this.$message.error(result.message)
        }
      })
    },
    // 子类可以重写该函数
    doDelete() {
      this._doDelete()
    },
    _doDelete() {
      this.deleting = { delay: 50 }
      this.$http.post(this.deleteUrl + this.pageParams.id).then((rs) => {
        this.deleting = false
        //
        const result = rs.data
        if (result.success) {
          this.$message.success(result.message);
          this.doSuccess(null)
        } else {
          this.$message.error(result.message)
        }
      })
    },
    doHelp() {
      this.helpVisible = true
    },
    // 自动生成树形结构的全称字段
    doAutoFullName() {
      if (this.formData.name) {
        if (this.formData.parentFullName) {
          this.formData.fullName = this.formData.parentFullName + ' / ' + this.formData.name
        }else if (this.formData.parentName) {
          this.formData.fullName = this.formData.parentName + ' / ' + this.formData.name
        } else {
          this.formData.fullName = this.formData.name
        }
      }
    },
    doSuccess(data) {
      this.$store.dispatch('ctx/doActionSuccess', {
        data: data
      })
      this.$emit('onSuccess', data)
      if (this.pageParams.id) {
        this.$emit("onClose", data)
        this.$store.dispatch('ctx/doActionEnd', {
          data: data
        })
      }
    },
    doClose() {
      this.$emit("onClose")
    }
  }
}